﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using test.demo.Model.WM仓储管理;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Jiangzhenying.ReturnOrders;
using wangyaxin.Jiangzhenying.ReturnOrders.Dto;
using wangyaxin.Jiangzhenying.UnitInfos.Dto;
using wangyaxin.Jiangzhenying.WorkstationWorkers.Dto;
using wangyaxin.Wang.Dto;

namespace wangyaxin.Jiangzhenying
{
    [ApiExplorerSettings(GroupName = "江珍莹")]
    public class ReturnOrderService : ApplicationService, IReturnOrderIService
    {
        private readonly IRepository<ReturnOrder, int> _repository;
        public ReturnOrderService(IRepository<ReturnOrder, int> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取退回订单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/GetReturnOrderNo")]
        public async Task<string> GetReturnOrderNo(string? rtCode, string? rtName, string? soCode, string? clientName)
        {
            var works = await _repository.GetListAsync();
            if(!string.IsNullOrEmpty(rtCode))
            {
                works = works.Where(x => x.RtCode.Contains(rtCode)).ToList();
            }
            if (!string.IsNullOrEmpty(rtName))
            {
                works = works.Where(x => x.RtName.Contains(rtName)).ToList();
            }
            if (!string.IsNullOrEmpty(soCode))
            {
                works = works.Where(x => x.SoCode.Contains(soCode)).ToList();
            }
            if (!string.IsNullOrEmpty(clientName))
            {
                works = works.Where(x => x.ClientName.Contains(clientName)).ToList();
            }
            var worksDto = ObjectMapper.Map<List<ReturnOrder>, List<ReturnOrderDto>>(works);
            return JsonConvert.SerializeObject(new PageDto<ReturnOrderDto>
            {
                code = "", // 可以根据需要设置状态码
                data = worksDto, // 返回添加的用户信息
                msg = "获取成功", // 可以根据需要添加消息
                count = works.Count // 添加的用户数量  
            });
        }

        /// <summary>
        /// 添加退回订单
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/AddReturnOrder")]
        public async Task<int> AddReturnOrder(ReturnOrder input)
        {
            // 将返回格式统一
            var workDto = ObjectMapper.Map<ReturnOrder, ReturnOrder>(input);
            workDto.ClientId = 1;
            workDto.WarehouseId = 1;
            workDto.AreaId = 1;
            workDto.LocationId = 1;
            workDto.CreateBy = "孙宝硕";
            workDto.CreateTime = DateTime.Now;
            await _repository.InsertAsync(workDto);
            return 1;
        }

        /// <summary>
        /// 删除退回订单
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/DeletReturnOrder")]
        public async Task<string> DeletReturnOrder(int id)
        {
            var returnOrder=await _repository.GetAsync(x=>x.Id==id);
            await _repository.DeleteAsync(returnOrder,true);
            return JsonConvert.SerializeObject(new PageDto<ReturnOrderDto> { code = "200", data = null, msg = "成功", count = 1 });
        }


        /// <summary>
        /// 根据id获取退回订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/GetReturnOrder")]
        public async Task<ReturnOrder> GetReturnOrder(int id)
        {
            var works = await _repository.GetAsync(id);
            return ObjectMapper.Map<ReturnOrder, ReturnOrder>(works);
        }

        /// <summary>
        /// 更新退回订单
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/UpdateReturnOrder")]
        public async Task<int> UpdateReturnOrder(UpdateReturnOrderDto input)
        {
            var wang = await _repository.GetAsync(x => x.Id == input.Id);
            wang.RtCode = input.RtCode;
            wang.RtName = input.RtName;
            wang.SoCode = input.SoCode;
            wang.ClientId = input.ClientId;
            wang.ClientCode = input.ClientCode;
            wang.ClientName = input.ClientName;
            wang.WarehouseId = input.WarehouseId;
            wang.WarehouseCode = input.WarehouseCode;
            wang.WarehouseName = input.WarehouseName;
            wang.LocationId = input.LocationId;
            wang.LocationCode = input.LocationCode;
            wang.LocationName = input.LocationName;
            wang.AreaId = input.AreaId;
            wang.AreaCode = input.AreaCode;
            wang.AreaName = input.AreaName;
            wang.RtDate = input.RtDate;
            wang.RtReason = input.RtReason;
            wang.Status = input.Status;
            wang.Remark = input.Remark;
            wang.CreateBy = input.CreateBy;
            wang.CreateTime = input.CreateTime;
            wang.UpdateBy = "孙宝硕";
            wang.UpdateTime = DateTime.Now;
            await _repository.UpdateAsync(wang);
            return 1;
        }
    }
}
